package org.example.writable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName FlowMapper
 * @Author 小坏
 * @Date 2021/7/16、17:10
 * @Version 1.0
 * <p>
 * 1、输入的 K,V
 * LongWritable, Text(一行)
 * <p>
 * 1、输出的 K,V
 * Text(手机号), FlowBean(FlowBean对象)
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text outK = new Text();
    private FlowBean outV = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        /**
         * -----------------1  电话        ip       地址        上流量      下流量      总流量--------------------
         *                  1 159940    192.168.2   上海            2          30         32
         */
        //TODO 1、获取一行
        String lien = value.toString();

        //TODO 2、切割
        String[] split = lien.split("\t");

        //TODO 3、抓取想要的数据
        //手机号
        //上流量 和下 流量
        String phone = split[1];
        String up = split[split.length - 3];
        String down = split[split.length - 2];

        //TODO 4、封装
        outK.set(phone);
        outV.setUpFlow(Long.parseLong(up));
        outV.setDownFlow(Long.parseLong(down));
//        outV.setSumFlow();

        //TODO 5、写出
        context.write(outK,outV);
    }

    public static void main(String[] args) {
        String a = "1\t电话\tip\t地址\t上流量\t下流量\t总流量";

        String[] split = a.split("\t");
        String phone = split[1];
        String up = split[split.length - 3];
        String down = split[split.length - 2];
        String coutn = split[split.length - 1];

        System.out.println(phone);
        System.out.println(up);
        System.out.println(down);
        System.out.println(coutn);

    }
}
